using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace FingerPoint.Android.UI
{
    using FingerPoint.Android.Core;

    public class FPReticle : MonoBehaviour
    {
        [SerializeField]
        private Image outside;

        [SerializeField]
        private Image inside;
        private CanvasGroup selfGroup;
        private RectTransform selfRect;
        private bool isShowing = false;
        private bool hasHand = false;
        private float min;
        private float max = 0.8f;

        private void Awake()
        {
            selfGroup = GetComponent<CanvasGroup>();
            if (selfGroup == null)
            {
                selfGroup = gameObject.AddComponent<CanvasGroup>();
            }
            selfGroup.interactable = false;
            selfGroup.blocksRaycasts = false;
            selfGroup.alpha = 0;

            selfRect = GetComponent<RectTransform>();

            inside.enabled = false;
            outside.enabled = true;

            Provider.Instance.OnHandMetaFound += OnHandMetaFound;
            Provider.Instance.OnHandMetaLost += OnHandMetaFound;
        }

        private void OnDisable()
        {
            Provider.Instance.OnHandMetaFound -= OnHandMetaFound;
            Provider.Instance.OnHandMetaLost -= OnHandMetaFound;
        }

        private void OnHandMetaFound(HandMeta handMeta)
        {
            selfGroup.alpha = handMeta.hvisible;
            inside.enabled = false;
            outside.enabled = true;
            hasHand = handMeta.hvisible == 1 && handMeta.qnull == 0;
        }

        // Start is called before the first frame update
        void Start()
        {
            var canvasScaler = GetComponentInParent<CanvasScaler>();
            float scale;
            if (canvasScaler.referenceResolution.x > Screen.width)
            {
                scale = Screen.width / canvasScaler.referenceResolution.x;
            }
            else
            {
                scale = canvasScaler.referenceResolution.x / Screen.width;
            }
            max *= scale * 2;
            min = max * 0.8f;
            selfRect.localScale = Vector3.one * max;
            isShowing = true;
            selfGroup.alpha = 1;
#if UNITY_EDITOR
            hasHand = true;
#else
            hasHand = FInput.handPresent;
#endif
        }

        // Update is called once per frame
        void Update()
        {
            if (isShowing && hasHand)
            {
                RectTransformUtility.ScreenPointToLocalPointInRectangle(
                    selfRect.parent as RectTransform,
                    FInput.POI(),
                    null,
                    out Vector2 pos
                );
                selfRect.anchoredPosition = pos;
                if (FInput.GetFingerPick(0))
                {
                    selfRect.localScale = Vector3.one * min;
                    inside.enabled = true;
                    outside.enabled = false;
                }
                if (FInput.GetFingerDrop(0))
                {
                    selfRect.localScale = Vector3.one * max;
                    inside.enabled = false;
                    outside.enabled = true;
                }
            }
        }
    }
}
